package com.xhxi.photobooker.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xhxi.photobooker.entity.Portfolio;
import com.xhxi.photobooker.mapper.PortfolioMapper;
import com.xhxi.photobooker.service.PortfolioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 摄影师作品服务实现类
 */
@Service
public class PortfolioServiceImpl implements PortfolioService {
    
    @Autowired
    private PortfolioMapper portfolioMapper;
    
    @Override
    @Transactional
    public Portfolio savePortfolio(Portfolio portfolio) {
        Date now = new Date();
        portfolio.setCreateTime(now);
        portfolio.setUpdateTime(now);
        
        // imageUrls处理：如为List或数组，转为JSON字符串
        try {
            Object imageUrls = portfolio.getImageUrls();
            if (imageUrls != null && !(imageUrls instanceof String)) {
                ObjectMapper mapper = new ObjectMapper();
                portfolio.setImageUrls(mapper.writeValueAsString(imageUrls));
            }
        } catch (Exception e) {
            throw new RuntimeException("图片URL序列化失败", e);
        }
        
        // 设置默认值
        if (portfolio.getStatus() == null) {
            portfolio.setStatus(0); // 默认草稿状态
        }
        if (portfolio.getViewCount() == null) {
            portfolio.setViewCount(0);
        }
        if (portfolio.getLikeCount() == null) {
            portfolio.setLikeCount(0);
        }
        if (portfolio.getIsFeatured() == null) {
            portfolio.setIsFeatured(0);
        }
        if (portfolio.getSortWeight() == null) {
            portfolio.setSortWeight(0);
        }
        
        portfolioMapper.insert(portfolio);
        return portfolio;
    }
    
    @Override
    public Portfolio findPortfolioById(Long id) {
        return portfolioMapper.selectById(id);
    }
    
    @Override
    public IPage<Portfolio> findPortfoliosByPhotographerId(Long photographerId, Integer page, Integer size) {
        Page<Portfolio> pageParam = new Page<>(page, size);
        return portfolioMapper.selectByPhotographerId(pageParam, photographerId);
    }
    
    @Override
    public List<Portfolio> findFeaturedPortfoliosByPhotographerId(Long photographerId, Integer limit) {
        return portfolioMapper.selectFeaturedByPhotographerId(photographerId, limit);
    }
    
    @Override
    public List<Portfolio> findPortfoliosByCategory(String category) {
        return portfolioMapper.selectByCategory(category);
    }
    
    @Override
    public List<Portfolio> findPortfoliosByTag(String tag) {
        return portfolioMapper.selectByTag(tag);
    }
    
    @Override
    @Transactional
    public Portfolio updatePortfolio(Long id, Portfolio portfolio) {
        Portfolio existingPortfolio = portfolioMapper.selectById(id);
        if (existingPortfolio == null) {
            throw new RuntimeException("作品不存在");
        }
        
        portfolio.setId(id);
        portfolio.setUpdateTime(new Date());
        portfolioMapper.updateById(portfolio);
        return portfolio;
    }
    
    @Override
    @Transactional
    public boolean deletePortfolio(Long id) {
        return portfolioMapper.deleteById(id) > 0;
    }
    
    @Override
    @Transactional
    public boolean setFeatured(Long id, boolean isFeatured) {
        Portfolio portfolio = new Portfolio();
        portfolio.setId(id);
        portfolio.setIsFeatured(isFeatured ? 1 : 0);
        portfolio.setUpdateTime(new Date());
        return portfolioMapper.updateById(portfolio) > 0;
    }
    
    @Override
    @Transactional
    public boolean incrementViewCount(Long id) {
        return portfolioMapper.incrementViewCount(id) > 0;
    }
    
    @Override
    @Transactional
    public boolean incrementLikeCount(Long id) {
        return portfolioMapper.incrementLikeCount(id) > 0;
    }
    
    @Override
    @Transactional
    public boolean decrementLikeCount(Long id) {
        return portfolioMapper.decrementLikeCount(id) > 0;
    }
    
    @Override
    @Transactional
    public boolean updateStatus(Long id, Integer status) {
        Portfolio portfolio = new Portfolio();
        portfolio.setId(id);
        portfolio.setStatus(status);
        portfolio.setUpdateTime(new Date());
        return portfolioMapper.updateById(portfolio) > 0;
    }
    
    @Override
    @Transactional
    public boolean updateSortWeight(Long id, Integer sortWeight) {
        Portfolio portfolio = new Portfolio();
        portfolio.setId(id);
        portfolio.setSortWeight(sortWeight);
        portfolio.setUpdateTime(new Date());
        return portfolioMapper.updateById(portfolio) > 0;
    }
} 